import { Button, Form, Radio, Select, DatePicker, Card, Table } from "antd";
import locale from "antd/es/date-picker/locale/zh_CN";
import { useState, useEffect } from "react";
import { columns } from "./Table";
import { http } from "@/utils";

const { Option } = Select;
const Article = () => {
  const { RangePicker } = DatePicker;

  //频道数据
  const [channels, setChannels] = useState([]);
  useEffect(() => {
    async function fetchChannels() {
      const res = await http.get("/channels");
      setChannels(res.data.data.channels);
    }
    fetchChannels();
  }, []);
  //表格
  const [article, setArticleList] = useState({
    list: [],
    count: 0,
  });

  // 参数管理
  const [params, setParams] = useState({
    page: 1,
    per_page: 10,
  });
  useEffect(() => {
    async function fetchArticleList() {
      const res = await http.get("/mp/articles", { params });
      const { results, total_count } = res.data;
      setArticleList({
        list: results,
        count: total_count,
      });
    }
    fetchArticleList();
  }, [params]);

  // 筛选功能
  const onSearch = (values) => {
    const { status, channel_id, date } = values;
    // 格式化表单数据
    const _params = {};
    // 格式化status
    _params.status = status;
    if (channel_id) {
      _params.channel_id = channel_id;
    }
    if (date) {
      _params.begin_pubdate = date[0].format("YYYY-MM-DD");
      _params.end_pubdate = date[1].format("YYYY-MM-DD");
    }
    // 修改params参数 触发接口再次发起
    setParams({
      ...params,
      ..._params,
    });
  };

  
const pageChange = (page) => {
  // 拿到当前页参数 修改params 引起接口更新
  setParams({
    ...params,
    page
  })
}

  return (
    <>
      <Form onFinish={onSearch}  initialValues={{ status: null }}>
        <Form.Item label="状态" name="status">
          <Radio.Group >
            <Radio value={null}>全部</Radio>
            <Radio value={0}>草稿</Radio>
            <Radio value={1}>待审核</Radio>
            <Radio value={2}>审核通过</Radio>
            <Radio value={3}>审核失败</Radio>
          </Radio.Group>
        </Form.Item>
        <Form.Item label="频道" name="channel_id">
          <Select placeholder="请选择文章频道" style={{ width: 200 }}>
            {channels?.map((item) => (
              <Option key={item.id} value={item.id}>
                {item.name}
              </Option>
            ))}
          </Select>
        </Form.Item>
        <Form.Item label="日期：" name="date">
          <RangePicker locale={locale} />
        </Form.Item>
        <Form.Item>
          <Button type="primary" htmlType="submit">
            筛选
          </Button>
        </Form.Item>
      </Form>
      <Card title={`根据筛选条件共查询到 ${article.count} 条结果：`}>
        <Table dataSource={article.list} columns={columns}   pagination={{
        position: ['bottomCenter'],
        current: params.page,
        pageSize: params.per_page,
        onChange: pageChange
      }}/>
      </Card>
    </>
  );
};
export default Article;
